package com.account.book.config;

import com.account.book.common.handler.AdminInterceptor;
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @author liang-hw
 * @className WebMvcConfig
 * @description @Configuration
 * @date 2023/11/08 14:34:20
 */
@Configuration
@Slf4j
@EnableSwagger2
@EnableKnife4j
public class WebMvcConfig implements WebMvcConfigurer {
    @Value("${spring.application.name}")
    private String applicationName;

    /**
     * @description:设置静态资源映射(静态资源不放到static中需要进行配置)
     * @param: [registry]
     * @return: void
     **/
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        log.info("开始进行静态资源映射...");
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    /**
     * 配置拦截器
     *
     * @param interceptorRegistry
     */
    @Override
    public void addInterceptors(InterceptorRegistry interceptorRegistry) {
        // 拦截所有 /admin/** 的访问地址
        interceptorRegistry.addInterceptor(new AdminInterceptor()).addPathPatterns("/admin/**");
    }

    @Bean
    public Docket defaultApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName(applicationName)
                .select()
                // 添加@Api注解才显示
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                // 这里指定Controller扫描包路径
                .apis(RequestHandlerSelectors.basePackage("com.account.book"))
                .paths(PathSelectors.any())
                .build();
    }

    /**
     * @param
     * @return @return {@link ApiInfo }
     * @author liang-hw
     * @description swagger-api接口描述信息
     * @date 2023/11/08 14:20:14
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("API文档")
                .description("API文档")
                .contact(
                        new Contact(
                                "liang-hw",
                                "https://gitee.com/liang-huwei",
                                "15608227372@163.com"
                        )
                )
                .version("1.0.0")
                .build();
    }
}
